'use strict'

class admin {
  constructor () {

  }
  // 得到管理员列表
  async list (c) {
    let muser = c.service.model.admin
    let ulist = await muser.list()
    c.send(ulist)
  }

  //得到一个管理员
  async get (c) {
    if (c.param.id === 'self') {
      c.param.id = c.box.user.id
      let muser = c.service.model.admin
      let ulist = await muser.get(c.param.id)
      // console.log(ulist)
      c.send({
        username:ulist[0].username,
        id:ulist[0].id,
        nickname:ulist[0].nickname,
        sex:ulist[0].sex,
        birth:ulist[0].birthday,
        email:ulist[0].email,
        words:ulist[0].words,
        addr:ulist[0].addr,
        position:ulist[0].position,
        time:ulist[0].time,
        descrip:ulist[0].descrip
      })
    }
  }

  //插入数据
  async post(c){
    let data= JSON.parse(c.body)
    let result = c.service.model.admin
    await result.insert(data)
  }

  //删除数据
  async delete(c){
    let wuser = c.service.model.admin
    await wuser.delete(c.param.id)
  }

  //修改数据
  async put(c){
    let {username,nickname,sex,birthday,email,words,addr,position,time,descrip} = JSON.parse(c.body)
    let id=c.param.id
    let data={username,nickname,sex,birthday,email,words,addr,position,time,descrip}
    let wuser =c.service.model.admin
    await wuser.update(data,id)
  }

}

module.exports = admin
